package com.example.demo.controller;

import com.example.demo.bean.Goods;
import com.example.demo.bean.Order;
import com.example.demo.bean.User;
import com.example.demo.bean.view.OrderView;
import com.example.demo.bean.view.Result;
import com.example.demo.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.xpath.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.List;

@Controller
public class OrderController {
    @Autowired(required = true)
    @Qualifier("OrderService")
    private OrderService orderService;

    @RequestMapping("/insertOrder")
    @ResponseBody
    public Result<?> insertOrder() {
        Order order = new Order();
        Goods goods = new Goods();
        User buyer = new User();
        buyer.setUserNum("test");
        goods.setGoodsNum("test");
        goods.setSellerNum("seller");
        String timeStr = "2010-06-23 13:18:33.112233";
        Timestamp ts = Timestamp.valueOf(timeStr);
        String OrderId = System.currentTimeMillis() + "";
        order.setOrderNum(OrderId);
        order.setBuyerNum(buyer.getUserNum());
        order.setSellerNum(goods.getSellerNum());
        order.setOrderCondition(1);
        order.setDeleteFlag(0);
        order.setTimestamp(ts);
        order.setNumOfGoods(1);
        order.setGoodsNum(goods.getGoodsNum());
        if(orderService.insertOrder(order)){
            return Result.success();
        }
        else
        {
            return Result.error("","插入订单失败");
        }
    }

    @RequestMapping("/deleteOrderForAdmin")
    @ResponseBody
    public Result<?> deleteOrderForAdmin() {
        Order order = new Order();
        order.setOrderNum("1627020135229");
        if(orderService.deleteOrderForAdmin(order))
        {
            return Result.success();

        }
        else
        {
            return Result.error("", "删除订单失败");
        }
    }

    @RequestMapping("/deleteOrderForUser")
    @ResponseBody
    public Result<?> deleteOrderForUser() {
        Order order = new Order();
        Order laOrder = new Order();
        laOrder.setDeleteFlag(1);
        order.setOrderNum("1627020191081");
        if(orderService.deleteOrderForUser(order,laOrder))
        {
            return Result.success();
        }
        else
        {
            return Result.error("", "用户删除订单失败");
        }
    }

    @RequestMapping("/updateOrderCondition")
    @ResponseBody
    public Result<?> updateOrder() {
        Order order = new Order();
        Order laOrder = new Order();
        laOrder.setDeleteFlag(1);
        laOrder.setOrderCondition(2);
        order.setOrderNum("1627020191081");
        if(orderService.updateOrder(order,laOrder))
        {
            return Result.success();
        }
        else
        {
            return Result.error("", "更新订单失败");
        }

    }

    @RequestMapping("/findAllOrder")
    @ResponseBody
    public Result<?> findAllOrder() {
        List<Order> orderList = orderService.findAllOrder();
        if(orderList != null) {
            PageHelper.startPage(1, 1);
            PageInfo<Order> info = new PageInfo<>(orderList);
            return Result.success(info);
        }
        else
        {
            return Result.error("","查找订单失败");
        }
    }
    @RequestMapping("/findOrderByBuyerNum")
    @ResponseBody
    public Result<?> findOrderByBuyerNum() {//搜索为完善，应根据标识位进行搜索
        User buyer = new User();
        buyer.setUserNum("test");
        List<OrderView> orderViewList = orderService.findOrderByBuyerNum(buyer);
        if(orderViewList != null) {
            PageHelper.startPage(1,1);
            PageInfo<OrderView> info = new PageInfo<>(orderViewList);
            return Result.success(info);
        }
        else{
            return Result.error("", "搜索订单失败通过id");
        }
    }

    @RequestMapping("/findOrderBySellerNum")
    @ResponseBody
    public Result<?> findOrderBySellerNum() {
        User seller = new User();
        seller.setUserNum("test");
        List<OrderView> orderViewList = orderService.findOrderBySellerNum(seller);
        if(orderViewList != null) {
            PageHelper.startPage(1,1);
            PageInfo<OrderView> info = new PageInfo<>(orderViewList);
            return Result.success(info);
        }
        else{
            return Result.error("", "findOrderBySellerNum失败");
        }
    }

    @RequestMapping("/findOrderByOrderNum")
    @ResponseBody
    public Result<?> findOrderByOrderNum() {
        Order order = new Order();
        order.setOrderNum("1627020191081");
        OrderView orderView = orderService.findOrderByOrderNum(order);
        if(orderView != null)
        {
            return Result.success(orderView);
        }
        else
        {
            return Result.error("", "findOrderByOrderNum失败");
        }
    }

    @RequestMapping("/findAllOrderView")
    @ResponseBody
    public Result<?> findAllOrderView() {

        List<OrderView> orderViewList = orderService.findAllOrderView();
        if(orderViewList != null)
        {
            PageHelper.startPage(1,1);
            PageInfo<OrderView> info = new PageInfo<>(orderViewList);
            return Result.success(info);
        }
        else
        {
            return Result.error("", "findAllOrderView失败");
        }
    }
}
